import { http, HttpResponse } from 'msw'

export const shortcuts = [
  { id: 1, name: 'Copy', command: 'Ctrl+C', description: 'Copy selected text' },
  { id: 2, name: 'Paste', command: 'Ctrl+V', description: 'Paste copied text' },
  { id: 3, name: 'Cut', command: 'Ctrl+X', description: 'Cut selected text' }
]

export const handlers = [
  http.get('/mock/shortcuts', () => {
    return HttpResponse.json({
      code: 200,
      data: shortcuts,
      message: 'Success'
    })
  }),

  http.post('/mock/shortcuts', async ({ request }) => {
    const newShortcut = await request.json()
    newShortcut.id = Math.floor(Math.random() * (10000 - 100 + 1)) + 100
    shortcuts.push(newShortcut)
    return HttpResponse.json({
      code: 200,
      data: newShortcut,
      message: 'Shortcut added successfully'
    })
  }),

  http.put('/mock/shortcuts/:id', async ({ params, request }) => {
    const updatedShortcut = await request.json()
    const index = shortcuts.findIndex((s) => s.id === parseInt(params.id))
    if (index !== -1) {
      shortcuts[index] = updatedShortcut
      return HttpResponse.json({
        code: 200,
        data: updatedShortcut,
        message: 'Shortcut updated successfully'
      })
    }
    return new HttpResponse(
      JSON.stringify({
        code: 404,
        message: 'Shortcut not found'
      }),
      { status: 404 }
    )
  }),

  http.delete('/mock/shortcuts/:id', ({ params }) => {
    const id = parseInt(params.id)
    const index = shortcuts.findIndex((s) => s.id === id)
    if (index !== -1) {
      shortcuts.splice(index, 1)
      return HttpResponse.json({
        code: 200,
        message: 'Shortcut deleted successfully'
      })
    }
    return new HttpResponse(
      JSON.stringify({
        code: 404,
        message: 'Shortcut not found'
      }),
      { status: 404 }
    )
  })
]
